package services

import (
	"context"
	"time"

	"gorm.io/gorm"

	datasource "go-caipu/pkg/services/datasources"
)

type Service struct {
	store store
}

func ProvideService(db *gorm.DB) (*Service, error) {
	store := ProvideStore(db)
	s := &Service{
		store: store,
	}
	return s, nil
}

func (s *Service) GetDatasourcePage(ctx context.Context, cmd datasource.GetDatasourcePageCommand) (result []datasource.Datasource, num int64, err error) {
	result, num, err = s.store.GetDatasourcePage(ctx, cmd)
	if err != nil {
		return
	}
	return
}

func (s *Service) Get(ctx context.Context, cmd datasource.GetDatasourceCommand) (datasource.Datasource, error) {
	return s.store.Get(ctx, cmd)
}
func (s *Service) GetDataSourceByType(ctx context.Context, cmd string) (datasource.Datasource, error) {
	return s.store.GetByName(ctx, cmd)
}

func (s *Service) Insert(ctx context.Context, cmd datasource.Datasource) (datasource.Datasource, error) {
	return s.store.Insert(ctx, cmd)
}

func (s *Service) Update(ctx context.Context, cmd datasource.Datasource) error {
	cmd.UpdatedAt = time.Now()
	return s.store.Update(ctx, cmd)
}

func (s *Service) Remove(ctx context.Context, cmd datasource.RemoveDatasourceCommand) error {
	return s.store.Remove(ctx, cmd)
}

func (s *Service) RemoveDSByPluginID(ctx context.Context, pluginID string, categorys []int64, apiIds []int64, menuIDs []int64) (err error) {
	return s.store.RemoveDSByPluginID(ctx, pluginID, categorys, apiIds, menuIDs)
}
